#include <iostream>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <vector>
#include <math.h>
#include <dirent.h>
#include <filesystem>
#include<algorithm>
using namespace std;
int strToInt(char*);
string nameGen(char* ,char* ,int ,char*);
void writeFile(string , vector<string>);
string findInFile(string ,string ,string , string);
vector<string> getFlags(string ,string);
void SHFlagsMaker(string*);
int main(int argc , char* argv[])
{
    vector<string> Flags[4];
    string prts[] = {"h0" ,"HH" ,"A0" ,"Hp"} , paths[] = {"FH_H1_flags.txt" ,"FH_H2_flags.txt" ,"FH_H3_flags.txt" ,"FH_Hp_flags.txt"} , fpaths[] = {"" , "" , "" , ""};
    string path = "" , tmp;
    for(int i = 0 ; argv[1][i] != '\0' ; i++)
    {
        path += argv[1][i];
        for(int j = 0 ; j < 4 ; j++)
            fpaths[j] += argv[1][i];
    }
    path += "Feynhiggs-output/";
    for(int i = 0 ; i < 4 ; i++)
    {
        Flags[i] = getFlags(path , prts[i]);
        tmp = "";
        tmp +="BR(";
        tmp += prts[i];
        tmp += " -> SUSY)";
        Flags[i].push_back(tmp);
        fpaths[i] += paths[i];
        ofstream file(fpaths[i].c_str());
        for(int j = 0 ; j < Flags[i].size() ; j++)
            file<<Flags[i][j]<<endl;
        file.close();
    }
    return 0;
}
int strToInt(char *in)
{
    char c[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    int result = 0;
    for(int i = 0 ; in[i] != '\0' ; i++)
    {
        for(int j = 0 ; j < 10 ; j++)
        {
            if(in[i] == c[j])
            {
                result = result * 10 + j;
                break;
            }
        }
    }
    return result;
}
string nameGen(char *path ,char *name ,int index ,char *type)
{
    string result;
    for(int i = 0 ; path[i] != '\0' ; i++)
        result += path[i];
    for(int i = 0 ; name[i] != '\0' ; i++)
        result += name[i];
    result += "_";
    result += to_string(index);
    for(int i = 0 ; type[i] != '\0' ; i++)
        result += type[i];
    return result;
}

vector<string> getFlags(string path , string particle)
{
    vector<string> flags , temps ;
    string tmp = " BR(";
    tmp += particle;
    tmp += " ->";
    string temp;
    DIR *d;
    struct dirent *dir;
    d = opendir(path.c_str());
    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
            if(strcmp(dir -> d_name , ".") != 0  && strcmp(dir -> d_name , "..") != 0)
            {
                string name = "";
                for(int i = 0 ; path[i] != '\0' ; i++)
                name += path[i];
                for(int i = 0 ; (dir->d_name)[i] != '\0' ; i++)
                name += (dir->d_name)[i];
                ifstream file(name);
                while (getline (file, temp))
                {
                    string str = "";
                    if(temp.find(tmp) != string::npos)
                    {
                        bool flag = false;
                        for(int i = 0 ; i < temp.length() ; i++)
                        {
                            if(temp[i] == 'B')
                                flag = true;
                            if(flag == true)
                                str += temp[i];
                        }
                    }
                    if(str.length() > 0)
                    {
                        bool check = false;
                        for(int i = 0 ; i < temps.size() ; i++)
                        {
                            if(str.compare(temps[i]) == 0)
                                check = true;
                        }
                        if(check == false)
                            temps.push_back(str);
                    }
                }
                file.close();
            }
        }
        closedir(d);
    }
    sort(temps.begin() , temps.end());
    for(int i = 0 ; i < temps.size() ; i++)
    {
        flags.push_back(temps[i]);
    }
    return flags;
}















